package com.excel;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import jxl.format.Alignment;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.write.Border;
import jxl.write.Label;
import jxl.write.VerticalAlignment;
import jxl.write.WritableCellFormat;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;


public class PositionExcelWriter extends ExcelWriter {
	/**
	 * 数据格式10,10
	 */
	Map<String, String> positionMap = new HashMap<String, String>();

	public PositionExcelWriter(String fileName, Map<String, String> positionMap) {
		super(fileName);
		this.positionMap = positionMap;
	}

	/**
	 * 通过HTML Table设置配置信息
	 */
	public void initConfig() {
		dataRowCount = 100;
	}

	protected void drawRect(BorderLineStyle style, Colour BorderColor,
			Colour bgColor) throws WriteException {
	}

	protected void writeTableHead(MergedColumns mergedColumns) {
	}

	/**
	 * 判断字符串是否是整数
	 */
	public static boolean isInteger(String value) {
		try {
			Integer.parseInt(value);
			return true;
		} catch (NumberFormatException e) {
			return false;
		}
	}

	/**
	 * 判断字符串是否是浮点数
	 */
	public static boolean isDouble(String value) {
		try {
			Double.parseDouble(value);
			if (value.contains("."))
				return true;
			return false;
		} catch (NumberFormatException e) {
			return false;
		}
	}

	/**
	 * 判断字符串是否是数字
	 */
	public static int getNumberFlag(String value) {
		int falg = 0;
		if (isDouble(value)) {
			falg = 1;
		} else if (isInteger(value)) {
			falg = 2;
		}
		return falg;
	}

	/**
	 * 起始位置从(2,0)开始
	 * 
	 * @param resultSet
	 */
	protected void writeDataSheet() {
		int rows = 0;
		Iterator iter = positionMap.keySet().iterator();
		
		int colIndex = this.tableXIndex + 1;
		// 标题列颜色
		String color = "#eeeeee";
		boolean isComplexHead = false;

		try {

			while(iter.hasNext()) {
				String position = (String) iter.next();
				int top = 0;
				int left = 0;
				String strs[] = position.split(",");
				top = Integer.parseInt(strs[0].trim());
				left = Integer.parseInt(strs[1].trim());
				String contentString;
				BorderLineStyle style = BorderLineStyle.THIN;
				Colour BorderColor = Colour.BLACK;
				WritableCellFormat alignStyle = new WritableCellFormat(); // 单元格样式
				alignStyle.setVerticalAlignment(VerticalAlignment.CENTRE);// 设置对齐方式
				alignStyle.setBorder(Border.TOP, style, BorderColor);// 设置边框的颜色和样式
				alignStyle.setBorder(Border.LEFT, style, BorderColor);// 设置边框的颜色和样式
				alignStyle.setBorder(Border.RIGHT, style, BorderColor);// 设置边框的颜色和样式
				alignStyle.setBorder(Border.BOTTOM, style, BorderColor);// 设置边框的颜色和样式
				// 如果是标题列，加背景, 设置默认对齐方式
				// 单元格对齐属性
				Alignment dataAlignment = Alignment.LEFT;
				Alignment titleAlignment = Alignment.CENTRE;
				alignStyle.setAlignment(dataAlignment); // 设置对齐方式
				Label label = new Label(left, top, positionMap.get(position));
				label.setCellFormat(alignStyle);
				s.addCell(label);

			}
		} catch (RowsExceededException e) {
			e.printStackTrace();
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (WriteException e) {
			e.printStackTrace();
		}

	}

	public MergedColumns getMergedColumns() {
		return mergedColumns;
	}

	public void setMergedColumns(MergedColumns mergedColumns) {
		this.mergedColumns = mergedColumns;
	}

}
